package com.feishi.core.util.http;

import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: caixq
 * Date: 2018-09-03
 * Time: 上午9:27
 */
public class HttpClientPoll {
    public static Queue<CloseableHttpClient> usingClients = new LinkedBlockingQueue<>();
    public static Queue<CloseableHttpClient> usedClients = new LinkedBlockingQueue<>();

    public static CloseableHttpClient getHttpClient() {
        synchronized (usedClients) {
            synchronized (usingClients) {
                CloseableHttpClient client;
                if (usedClients.isEmpty()) {
                    client = HttpClients.createDefault();
//                   client.getConnectionManager()

                } else {
                    client = usedClients.poll();
                }
                usingClients.offer(client);
                return client;
            }
        }
    }


    public static String sendRequest(HttpRequestBase request) throws IOException {
        CloseableHttpClient httpClient = null;
        try {
            httpClient = getHttpClient();
            CloseableHttpResponse response = httpClient.execute(request);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                if (response.getEntity() == null) {
                    return null;
                }
                String result = EntityUtils.toString(response.getEntity());
                return result;
            }

        } catch (Exception e) {
            throw e;
        } finally {
//            httpClient.close();
            //连接不关闭，留给新的请求使用，否则端口很快就会被完全占用
            request.releaseConnection();

            offer(httpClient);
        }
        return null;
    }

    private static void offer(CloseableHttpClient httpClient) {
        synchronized (usedClients) {
            synchronized (usingClients) {
                usingClients.remove(httpClient);
                usedClients.offer(httpClient);
            }
        }
    }
    /**
     * 清空httpclient，防止链接长期占用
     * */
    public static void clear(){
        if(usedClients.isEmpty()){
            return;
        }
        Queue<CloseableHttpClient> oldUsedClients;
        synchronized (usedClients){
            oldUsedClients =usedClients;
            usedClients=new LinkedBlockingQueue<>();
        }

        for (CloseableHttpClient oldUsedClient : oldUsedClients) {
            try {
                oldUsedClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                oldUsedClients.poll();
            }
        }

    }
}
